package com.example.demo.demos.web;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;

@Slf4j
@RestController
@RequiredArgsConstructor
public class AgvController {
    private final Map<String,Integer> mapper;
    ThreadPoolExecutor executor = new ThreadPoolExecutor(
            5,  // 核心线程数
            5,  // 最大线程数
            2,  // 当线程数大于核心线程数时，多余的空闲线程存活的最长时间
            TimeUnit.SECONDS,  // 存活时间单位
            new LinkedBlockingQueue<>(10)  // 任务队列，用来储存等待执行任务的队列
    );

    @PostMapping("/agv/storage/arrive")
    public void agvArriveLift(@RequestBody AgvLiftStorageLocationDto dto) {
        Integer integer = mapper.get("1");

        log.info("接口被触发integer:{}",integer);
    }

    @PostMapping("/lift/request")
    public void agvRequestLift(@RequestBody AgvApplyDto agvApplyDto) {
        log.info("请求电梯，分配电梯储位");
    }
    @Async
    @GetMapping("/lift/test")
    public Integer agvRequestLift1() throws ExecutionException, InterruptedException {
//        for (int i = 0; i < 100; i++) {
//            Thread.sleep(500);
//            System.out.println(Thread.currentThread().getName() + "当前任务:" + i);
//        }
//        for (int i = 0; i < 100; i++) {
//            Thread.sleep(500);
//            System.out.println(Thread.currentThread().getName() + "当前任务1:" + i);
//        }
        ThreadConfig threadConfig = new ThreadConfig();
        ThreadPoolExecutor executor = threadConfig.getExecutor();
        executor.execute(() ->{
            for (int i = 0; i < 100; i++) {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "当前任务:" + i);
            }
        });
        Future<String> submit = executor.submit(new Callable<String>() {
            int a = 0;

            @Override
            public String call() throws Exception {
                for (int i = 0; i < 100; i++) {
                        a = a + i;
                        Thread.sleep(500);
                    System.out.println(Thread.currentThread().getName() + "当前任务:" + i);
                }
                return Thread.currentThread().getName();
            }
        });
//        Integer integer = submit.get();
        executor.shutdown();
        return null;
    }
}
